package com.smilekay.serialtool;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class StringUtil {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static byte[] stringToByteArray(String strHexValue) {
        String[] strAryHex = strHexValue.split(" ");
        byte[] btAryHex = new byte[strAryHex.length];

        try {
            int nIndex = 0;
            String[] var7 = strAryHex;
            int var6 = strAryHex.length;

            for (int var5 = 0; var5 < var6; ++var5) {
                String strTemp = var7[var5];
                btAryHex[nIndex] = (byte) Integer.parseInt(strTemp, 16);
                ++nIndex;
            }
        } catch (NumberFormatException var8) {
        }

        return btAryHex;
    }

    public static byte[] stringArrayToByteArray(String[] strAryHex, int nLen) {
        if (strAryHex == null) {
            return null;
        } else {
            if (strAryHex.length < nLen) {
                nLen = strAryHex.length;
            }

            byte[] btAryHex = new byte[nLen];

            try {
                for (int i = 0; i < nLen; ++i) {
                    btAryHex[i] = (byte) Integer.parseInt(strAryHex[i], 16);
                }
            } catch (NumberFormatException var4) {
            }

            return btAryHex;
        }
    }

    public static String byteArrayToString(byte[] btAryHex, int nIndex, int nLen) {
        if (nIndex + nLen > btAryHex.length) {
            nLen = btAryHex.length - nIndex;
        }

        String strResult = String.format("%02X", btAryHex[nIndex]);

        for (int nloop = nIndex + 1; nloop < nIndex + nLen; ++nloop) {
            String strTemp = String.format(" %02X", btAryHex[nloop]);
            strResult = strResult + strTemp;
        }

        return strResult;
    }

    public static String[] stringToStringArray(String strValue, int nLen) {
        String[] strAryResult = null;
        if (strValue != null && !strValue.equals("")) {
            ArrayList<String> strListResult = new ArrayList();
            String strTemp = "";
            int nTemp = 0;

            int i;
            for (i = 0; i < strValue.length(); ++i) {
                if (strValue.charAt(i) != ' ') {
                    ++nTemp;
                    if (!Pattern.compile("^(([A-F])*([a-f])*(\\d)*)$").matcher(strValue.substring(i, i + 1)).matches()) {
                        return strAryResult;
                    }

                    strTemp = strTemp + strValue.substring(i, i + 1);
                    if (nTemp == nLen || i == strValue.length() - 1 && strTemp != null && !strTemp.equals("")) {
                        strListResult.add(strTemp);
                        nTemp = 0;
                        strTemp = "";
                    }
                }
            }

            if (strListResult.size() > 0) {
                strAryResult = new String[strListResult.size()];

                for (i = 0; i < strAryResult.length; ++i) {
                    strAryResult[i] = (String) strListResult.get(i);
                }
            }
        }

        return strAryResult;
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static char[] getChars(byte[] bytes, String encoding) {
        Charset cs = Charset.forName(encoding);
        ByteBuffer bb = ByteBuffer.allocate(bytes.length);
        bb.put(bytes);
        bb.flip();
        CharBuffer cb = cs.decode(bb);
        return cb.array();
    }

    /**
     * @deprecated
     */
    @Deprecated
    public static String toASCIIString(String hexString) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < hexString.length(); i += 2) {
            stringBuilder.append((char) Integer.parseInt(hexString.substring(i, i + 2), 16));
        }

        return stringBuilder.toString();
    }

    public static String hexStringToASCIIString(String hexString) {
        StringBuilder stringBuilder = new StringBuilder();
        if (hexString != null && !hexString.equals("")) {
            for (int i = 0; i < hexString.length(); i += 2) {
                char high = hexString.charAt(i);
                char low = hexString.charAt(i + 1);
                char no = (char) (charToInt(high) * 16 + charToInt(low));
                stringBuilder.append(no);
            }

            return stringBuilder.toString();
        } else {
            return null;
        }
    }

    private static int charToInt(char c) {
        int num = "0123456789ABCDEF".indexOf(String.valueOf(c));
        if (num < 0) {
            num = "0123456789abcdef".indexOf(String.valueOf(c));
        }

        return num;
    }

    public static byte[] subBytes(byte[] bytes, int start, int end) {
        byte[] subBytes = new byte[end - start];
        System.arraycopy(bytes, start, subBytes, 0, end - start);
        return subBytes;
    }

    public static int subBytesContains(byte[] parentBytes, byte[] childBytes, int startPosition) {
        if (parentBytes.length < childBytes.length) {
            return -1;
        } else {
            for (int i = startPosition; i < parentBytes.length; ++i) {
                for (int j = 0; j < childBytes.length && parentBytes[i + j] == childBytes[j]; ++j) {
                    if (j == childBytes.length - 1) {
                        return i;
                    }
                }
            }

            return -1;
        }
    }

    public static byte[] hexStringToBytes(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        if (hexString != null && !hexString.equals("")) {
            for (int i = 0; i < hexString.length(); i += 2) {
                char high = hexString.charAt(i);
                char low = hexString.charAt(i + 1);
                bytes[i / 2] = (byte) (charToInt(high) * 16 + charToInt(low));
            }

            return bytes;
        } else {
            return null;
        }
    }

    public static boolean compareBytes(byte[] first, byte[] second) {
        if (first.length != second.length) {
            return false;
        } else {
            for (int i = 0; i < first.length; ++i) {
                if (first[i] != second[i]) {
                    return false;
                }
            }

            return true;
        }
    }

    public static byte[] asciiStringToBytes(String string) {
        byte[] result = new byte[string.length()];

        for (int i = 0; i < string.length(); ++i) {
            result[i] = (byte) string.charAt(i);
        }

        return result;
    }
}
